/*
 * ControlUnitDialog.cpp - Template for Control Unit dialogs
 *
 * Copyright (C) 2008-2010 Comer352l
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ControlUnitDialog.h"


ControlUnitDialog::ControlUnitDialog(QString title, AbstractDiagInterface *diagInterface, QString language)
{
	// *** Initialize global variables:
	_language = language;
	_diagInterface = diagInterface;
	_SSMPdev = NULL;
	_infoWidget = NULL;
	_contentWidget = NULL;
	// Setup GUI:
	setupUi(this);
        backButton->hide();
	// Move window to desired coordinates:
	// Set window and dialog titles:
	title_label->setText(title);
	// Connect signals and slots:
        connect( closeButton, SIGNAL( released() ), this, SLOT( close() ) );
        connect( backButton, SIGNAL( released() ), this, SLOT( closeCurrWidg() ) );
}


ControlUnitDialog::~ControlUnitDialog()
{
        disconnect( closeButton, SIGNAL( released() ), this, SLOT( close() ) );
        disconnect( backButton, SIGNAL( released() ), this, SLOT( closeCurrWidg() ) );
	if (_infoWidget)
		delete _infoWidget;
	if (_contentWidget)
		delete _contentWidget;
	if (_SSMPdev)
	{
		disconnect( _SSMPdev, SIGNAL( commError() ), this, SLOT( communicationError() ) );
		delete _SSMPdev;
	}
	for (int k=0; k<_selButtons.size(); k++)
	{
		_selButtons.at(k)->disconnect();
		delete _selButtons.at(k);
	}
}

QPushButton * ControlUnitDialog::addFunction(int num, QString title, QIcon icon)
{
    QPushButton *button;
    switch (num) {
    case 1:
        button = Button1;
        break;
    case 2:
        button = Button2;
        break;
    case 3:
        button = Button3;
        break;
    case 4:
        button = Button4;
        break;
    case 5:
        button = Button5;
        break;
    }
    button->setIcon(icon);
    title = title.trimmed();
    button->setText(title);
    return button;
}

void ControlUnitDialog::setInfoWidget(QWidget *infowidget)
{
	if (_infoWidget)
		delete _infoWidget;
        infowidget->setParent(centralFrame);
        //information_groupBox->setMinimumHeight(infowidget->minimumHeight());
	_infoWidget = infowidget;
}


void ControlUnitDialog::setContentWidget(QString title, QWidget *contentwidget)
{
	if (_contentWidget)
		delete _contentWidget;
        centralFrame->hide();
        title2_label->setText(title);
        contentwidget->setParent(globalWidget);
        contentwidget->show();
        backButton->show();
        _contentWidget = contentwidget;
}

void ControlUnitDialog::closeCurrWidg()
{
    _contentWidget->hide();
    title2_label->setText("");
    backButton->hide();
    centralFrame->show();
}

QWidget * ControlUnitDialog::contentWidget()
{
	return _contentWidget;
}

SSMprotocol::CUsetupResult_dt ControlUnitDialog::probeProtocol(SSMprotocol::CUtype_dt CUtype)
{
	/* NOTE:  probe SSM2-protocol first !
	   If a serial pass through (K)KL-interface is used, the interface echo could be detected as a SSM1-ROM-ID,
	   if receive buffer flushing doesn't work reliable with the used serial port driver !
	*/
	SSMprotocol::CUsetupResult_dt result = SSMprotocol::result_commError;
	if ((CUtype == SSMprotocol::CUtype_Engine) || (CUtype == SSMprotocol::CUtype_Transmission))
	{
		// Probe SSM2-protocol:
		if (_diagInterface->connect(AbstractDiagInterface::protocol_SSM2))
		{
			_SSMPdev = new SSMprotocol2(_diagInterface, _language);
			result = _SSMPdev->setupCUdata( CUtype );
			if ((result == SSMprotocol::result_success) || (result == SSMprotocol::result_noOrInvalidDefsFile) || (result == SSMprotocol::result_noDefs))
				connect( _SSMPdev, SIGNAL( commError() ), this, SLOT( communicationError() ) );
			else
			{
				delete _SSMPdev;
				_SSMPdev = NULL;
				_diagInterface->disconnect();
			}
		}
	}
	if (_SSMPdev == NULL)
	{
		// Probe SSM1-protocol:
		if (_diagInterface->connect(AbstractDiagInterface::protocol_SSM1))
		{
			_SSMPdev = new SSMprotocol1(_diagInterface, _language);
			result = _SSMPdev->setupCUdata( CUtype );
			if ((result == SSMprotocol::result_success) || (result == SSMprotocol::result_noOrInvalidDefsFile) || (result == SSMprotocol::result_noDefs))
				connect( _SSMPdev, SIGNAL( commError() ), this, SLOT( communicationError() ) );
			else
			{
				delete _SSMPdev;
				_SSMPdev = NULL;
				_diagInterface->disconnect();
			}
		}
	}
	return result;
}


void ControlUnitDialog::communicationError(QString addstr)
{
	// Show error message
	if (addstr.size() > 0) addstr.prepend('\n');
        myMessage message(0);
        message.exec(0,tr("Communication Error:\n- No or invalid answer from Control Unit -") + addstr);
        close();
}


void ControlUnitDialog::closeEvent(QCloseEvent *event)
{
    if (_SSMPdev)
    {
        // Create wait message box:
        myProgressbar waitmsgbox(0,tr("Stopping Communication... Please wait !"));
        waitmsgbox.start();
        // Reset CU data:
        _SSMPdev->resetCUdata();
        // Close wait message box:
        waitmsgbox.stop();
    }
    event->accept();
}
